# Archive information
# $Id: makefile 10199 2012-02-28 20:16:30Z fished1 $
#
# Define the root of our build tree
BUILD_TREE_ROOT := $(CURDIR)
#export BUILD_TREE_ROOT

# Linux only...
ifndef OS
# Time expressed as the seconds of the year. More portable than seconds since
# the epoch: `date +%s'
COMPILE_START_TIME := $(shell date +\(%j\*24\*60\*60+%H\*60\*60+%M\*60+%S\))
COMPILE_CURRENT_TIME = `date  +\(%j\*24\*60\*60+%H\*60\*60+%M\*60+%S\)`
# Print the time difference between Tcurrent and Tstart. This function can be
# used after any target, as shown in the form: (see example)
# $(call print_timer,$(COMPILE_CURRENT_TIME),$(COMPILE_START_TIME))
# args: Tcurrent = $(1), Tstart = $(2)
define print_timer
@CT=$(1) && \
printf "Compilation total time: %.2d:%.2d (min:sec)\n" \
`echo "($$CT - $(2))/60" | bc` \
`echo "($$CT - $(2))%60" | bc`
endef
endif

# Default target
all:

# Include all rules & configuration info
include $(BUILD_TREE_ROOT)/build/config.mk

# What we can build from here
PARTS := shared_app subsystem 

# We need this (below) to "build" a directory
include $(BUILD_TREE_ROOT)/build/dirbuild.mk

# Now assemble the full list of libraries ... should be a better way to do this dynamically
include $(BUILD_TREE_ROOT)/build/libs.mk

# We must always have a set of .o file(s) which are built and
# require (pull in) libraries.  You can't just link a bunch of libraries
# together into a final executable, they are pulled in as-needed
# to resolve any unresolved external references.
# link in our libs to fill in (recursively) all the missing pieces.
# Systick is listed since it's not called by anything and wouldn't otherwise be linked in.
LINK_OBJECTS := $(addprefix $(ObjectDir)/, subsystem_methods.o subsystem_popdesign.o fillcode.o)

# Phony targets (Release and Debug can't be real targets if they're part of BuildDirs)
.PHONY: $(libraries) $(PARTS) all

# Define the default "target" and the output filename for this target
ProductOut := $(ExeDir)/code.axf

all: $(ProductOut)

LISTFILE       := $(ExeDir)/code.map
OBJECTINFOFILE := $(ExeDir)/ObjectInfo.txt
STRUCTINFOFILE := $(ExeDir)/StructInfo.txt
DEVCOMPARMAP   := $(BUILD_TREE_ROOT)/../tools/MapCreator/GalaxyParameters.txt
CALLFILE       := $(ExeDir)/callgraph.htm
INTELHEXFILE   := $(ExeDir)/main_code.intel.hex
CRCEDHEXFILE   := $(ExeDir)/main_code_crced.hex
RTLHEXFILE     := $(ExeDir)/main_code_RTL.hex
BUILDREVFILE   := $(BUILD_TREE_ROOT)/build_rev.txt

ifeq ($(BuildType), Release)
	SCATFILE       := $(BUILD_TREE_ROOT)/build/scatter.scat
else ifeq ($(BuildType), Debug)
	SCATFILE       := $(BUILD_TREE_ROOT)/build/scatter.scat    
else 
	SCATFILE       := $(BUILD_TREE_ROOT)/build/scatter_for_rtl.scat
endif
LINKOPTS       := --list=$(LISTFILE) --scatter=$(SCATFILE) --callgraph_file=$(CALLFILE)
 
#--entry=Reset_Handler

# How we build our final executable.
# "make" from this dir, or explicitly "make all" (since "all" is the
# default target) will cause $(product) to be built (it is a pre-requisite
# for "all").
#$(ProductOut): build/libs.mk
#$(ProductOut): makefile
$(ProductOut): $(BUILD_TREE_ROOT)/build/libs.mk
$(ProductOut): $(BUILD_TREE_ROOT)/makefile
$(ProductOut): $(HWInterfaceLibraryPaths)
$(ProductOut): $(LibraryPaths)
$(ProductOut): $(SCATFILE)
$(ProductOut): $(LINK_OBJECTS) | $(ExeDir)
	@echo Linking $@ in $(CURDIR) due to $?
	@echo link command:
	$(LINK.c) $(LINKOPTS) -o$(ProductOut)  $(LINK_OBJECTS)  $(LibraryPaths) $(HWInterfaceLibraryPaths)

# Dump information about the last successful build.
all:
ifndef OS
	@echo
ifeq ($(BuildType), Release)
	@echo Hex files, Memory Maps, DevComm files will not be updated for this build type. 
else ifeq ($(BuildType), Debug)
	@echo Hex files, Memory Maps, DevComm files will not be updated for this build type.
	# rm  $(ExeDir)/main_fw_new.udb
	/cadtools/apps/understand/3.0.622/scitools/bin/linux64/und create -db $(ExeDir)/main_fw.udb -languages c++
	/cadtools/apps/understand/3.0.622/scitools/bin/linux64/und -db $(ExeDir)/main_fw.udb add $(BUILD_TREE_ROOT)
	/cadtools/apps/understand/3.0.622/scitools/bin/linux64/und analyze	$(ExeDir)/main_fw.udb

else
	@echo Creating the object list:
	fromelf --text -v -s -a --expandarrays --output=$(OBJECTINFOFILE) $(ProductOut)
# If the next fromelf notices 2 load regions, it'll create a folder instead of a file as an output.
# protect against this by creating a file of the same name so it crashes.
	echo > $(INTELHEXFILE)
	fromelf --i32 --output $(INTELHEXFILE) $(ProductOut)
	@echo Creating the hex file
	perl ../tools/BuildTools/galaxy_hexcrc.pl -a 0x10000000 -c 0x10000448 -i $(INTELHEXFILE) -o $(CRCEDHEXFILE) -r $(RTLHEXFILE) -b $(BUILDREVFILE)
	@echo Creating the memory map and DevComm files:
	perl ../tools/MapCreator/MapCreator.pl $(OBJECTINFOFILE) $(DEVCOMPARMAP) $(BUILDREVFILE) $(ExeDir) Galaxy_m ""
	@tail --lines=9 $(LISTFILE)
	
endif
endif
	@echo Build is complete
	@echo Make version = $(MAKE_VERSION)
# Can only log build time on linux due to lack of date command on Windows.
ifndef OS	
	$(call print_timer,$(COMPILE_CURRENT_TIME),$(COMPILE_START_TIME))
endif	

# How we build clean.
# We only have to worry about things *not* built/created
# in sub-directories.
# Note that the subdirs will be cleaned automatically by virtue of
# dependencies called out in dirbuild.mk (the line which reads:
# clean: $(CLEANS)
# In our case, we only generate axf, callgraph, and a .map, so we need to remove those.
clean: | $(ExeDir)
	$(RM) $(call MakeOsPath, $(ProductOut) $(LISTFILE) $(CALLFILE) $(NULL_PIPE))
